<template>
  <el-container>
    <el-main>
      <div style="width: 100%;display: flex;align-items: flex-end;border-bottom: 1px #eee solid;margin-bottom: 20px">
        <span style="font-size: 18px;color:#409EFF;margin: 10px 0;">基本信息</span>
      </div>
      <el-form ref="sysJobForm" :model="sysJobForm" :rules="rules" label-width="100px">
        <el-col :span="14">
          <el-form-item label="任务名称">
            <el-col :span="12">
              <el-input v-model="sysJobForm.name" placeholder="请输入任务名称"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="任务描述">
            <el-col :span="12">
              <el-input v-model="sysJobForm.jobDesc" placeholder="请输入任务描述"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="执行时间">
            <el-col :span="12">
              <el-input v-model="sysJobForm.cron" placeholder="请输入执行时间"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="任务执行方法">
            <el-col :span="12">
              <el-input v-model="sysJobForm.clazzPath" placeholder="请输入任务执行方法"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="绑定IP地址">
            <el-col :span="12">
              <el-input v-model="sysJobForm.execAddr" placeholder="请输入绑定IP地址"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="状态">
            <el-col :span="12">
              <el-select v-model="sysJobForm.status" placeholder="请选择状态">
                <el-option label="禁用" value="0"></el-option>
                <el-option label="启用" value="1"></el-option>
              </el-select>
            </el-col>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="saveSysJob('sysJobForm')">新建任务</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-main>
  </el-container>
</template>
<script>
  import {save} from '@/api/sysJob'
  import {EventBus} from '@/api/eventBus'

  export default {
    name: 'sysJob-add',
    data() {
      return {
        sysJobForm: {
          name: '',
          jobDesc: '',
          cron: '',
          execAddr:'',
          status: '',
          execStatus: 0,
          clazzPath: ''
        },
        clicked: false,
        rules: {
          name: [
            {required: true, message: '请输入描述任务', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
          ],
          jobDesc: [
            {required: true, message: '请输入其他描述', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
          ],
          cron: [
            {required: true, message: '请输入任务表达式', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
          ],
          status: [
            {required: true, message: '请选择状态', trigger: 'blur'}
          ],
          execStatus: [
            {required: true, message: '请选择执行状态', trigger: 'blur'}
          ],
          clazzPath: [
            {required: true, message: '请输入任务执行方法', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
          ]
        },
      }
    },

    methods: {
      saveSysJob(sysJobForm) {
        this.$refs[sysJobForm].validate((valid) => {
          if (valid) {//校验表单
            this.clicked = true
            this.sysJobForm.execAddr = '192.168.137.1'//这里写执行任务的IP
            console.log(this.sysJobForm,'form')
            save(this.sysJobForm).then(res => {
              console.log(res)
              this.clicked = false
              this.$message({
                message: res.content,
                offset: 120,
                center: true
              });
              EventBus.$emit('sysJob-add', res.data)
              this.$store.dispatch('tagsView/delView', this.$route).then(({visitedViews}) => {
                this.$router.push({
                  path: '/sysJob/list'
                })
              })
            }).catch(err => {
              console.log(err)
              this.clicked = false
            })
          } else {
            console.log('error submit')
            return false
          }
        })
        console.log('submit!');
      }
    }
  }
</script>
